package com.derbysoft.dswitch.adapter.ihg.dswitch3.service.impl;

import com.derbysoft.dswitch.adapter.ihg.common.domain.ChannelAuthorization;
import com.derbysoft.dswitch.adapter.ihg.common.repository.ChannelAuthorizationRepository;
import com.derbysoft.dswitch.adapter.ihg.dto.Errors;
import com.derbysoft.dswitch.adapter.ihg.dto.OTAHotelAvailRS;
import com.derbysoft.dswitch.dto.common.KeyValue;
import com.derbysoft.dswitch.dto.hotel.reservation.ReservationRoomStayDTO;
import com.derbysoft.dswitch.remote.hotel.dto.ErrorDTO;
import com.derbysoft.dswitch.remote.hotel.dto.HotelReservationRequest;
import com.derbysoft.dswitch.remote.hotel.dto.HotelReservationResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * Created by: jason
 * Date: 2012-05-15
 */
public abstract class BaseHotelService {

    public static final String SYSTEM = "System";

    @Value("${provider}")
    protected String provider;

    @Value("${dswitch3.adapter}")
    protected String adapter;

    @Autowired
    private ChannelAuthorizationRepository channelAuthorizationRepository;

    protected ErrorDTO createError(Errors errors) {
        ErrorDTO errorDTO = new ErrorDTO();
        errorDTO.setSource(provider);
        errorDTO.setCode(errors.getError().get(0).getCode());
        StringBuilder builder = new StringBuilder();
        for (com.derbysoft.dswitch.adapter.ihg.dto.Error error : errors.getError()) {
            builder.append(error.getShortText()).append(" ");
        }
        errorDTO.setMessage(builder.toString());
        return errorDTO;
    }

    protected HotelReservationResponse noAllotmentResponse(OTAHotelAvailRS otaHotelAvailRS, HotelReservationRequest reservationRequest, String webserviceTime) {
        HotelReservationResponse reservationResponse = new HotelReservationResponse();
        reservationResponse.setTaskId(reservationRequest.getHeader().getTaskId());
        reservationResponse.setError(createError(otaHotelAvailRS, reservationRequest));
        reservationResponse.getElapsedTimes().add(new KeyValue(ElapsedTimeType.BEFORE_RESERVATION_AVAIL.name(), webserviceTime));
        return reservationResponse;
    }

    private ErrorDTO createError(OTAHotelAvailRS otaHotelAvailRS, HotelReservationRequest reservationRequest) {
        ErrorDTO error = new ErrorDTO();
        error.setSource(adapter);
        if (otaHotelAvailRS.getErrors() == null) {
            error.setCode("NoRate");
            ReservationRoomStayDTO roomStayDTO = reservationRequest.getHotelReservationRQ().getHotelReservation().getRoomStaysList().get(0);
            error.setMessage(String.format("No allotment for ratePlanCode[%s] roomTypeCode[%s] numberOfUnits[%d]",
                    roomStayDTO.getRatePlanCode(), roomStayDTO.getRoomTypeCode(), roomStayDTO.getNumberOfUnits()));
        } else {
            error.setCode(otaHotelAvailRS.getErrors().getError().get(0).getCode());
            error.setMessage(otaHotelAvailRS.getErrors().getError().get(0).getShortText());
        }
        return error;
    }

    protected ChannelAuthorization loadAuthorization(String channelPassport) {
        return channelAuthorizationRepository.loadByDerbyChannelCode(channelPassport);
    }
}
